package eteam.aps.service.impl;

import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.ProductInfoMapper;
import eteam.aps.mapper.ProductParamMapper;
import eteam.aps.model.dto.ProductInfoDto;
import eteam.aps.model.entity.ProductInfo;
import eteam.aps.model.entity.ProductParentEntity;
import eteam.aps.model.vo.ProductInfoListVo;
import eteam.aps.model.vo.ProductInfoVo;
import eteam.aps.service.ProductInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品 -- 装备信息业务层
 *
 * @author WMT
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ProductParamMapper productParamMapper;

    @Transactional
    @Override
    public int insert(ProductInfoDto productInfoDto) {
        return productInfoMapper.insert(productInfoDto);
    }

    @Transactional
    @Override
    public int delete(String fId) {
        return productInfoMapper.deleteByFId(fId);
    }

    @Transactional
    @Override
    public int update(ProductInfoDto productInfoDto) {
        ProductInfo equipInfo = new ProductInfo();
        BeanUtils.copyProperties(productInfoDto,equipInfo);
        equipInfo.setfLastModifyTime(LocalDateTime.now());
        return productInfoMapper.updateOne(equipInfo);
    }

    @Override
    public int selectCountByPeName(String equipName) {
        return productInfoMapper.selectCountByPeName(equipName);
    }

    @Override
    public int selectCountByPeCode(String equipCode) {
        return productInfoMapper.selectCountByPeCode(equipCode);
    }

    @Override
    public int selectFStateByFId(String fId) {
        return productInfoMapper.selectFStateByFId(fId);
    }

    @Override
    public List<ProductInfoVo> selectDetails(String fId) {
        List<ProductInfoVo> productInfoVos = productInfoMapper.selectDetails(fId);
        Map<String,Object> map = new HashMap<>(2);
        map.put("fDtlId",fId);
        map.put("fState",ParentEntity.FSTATE_DELETED);
        for (ProductInfoVo vo: productInfoVos) {
            vo.setProductParams(productParamMapper.selectAll(map));
        }
        return productInfoVos;
    }

    @Override
    public ProductInfo selectByFId(String fId) {
        return productInfoMapper.selectByFId(fId);
    }

    @Override
    public JsonPage<ProductInfoListVo> selectAll(Map<String, Object> map, int page, int pageSize, int fProductCategory) {
        map.put("fState",ParentEntity.FSTATE_DELETED);
        int totalCount = productInfoMapper.selectCountAll(map);
        List<ProductInfoListVo> list = null;
        if (totalCount > 0) {
            map.put("fProductCategory",fProductCategory);
            map.put("offset", (page - 1) * pageSize);
            map.put("limit", pageSize);
            list = productInfoMapper.selectAll(map);
        }
        return JsonPage.builder(page, pageSize, totalCount, list);
    }

    @Override
    public int selectCountAllByFEcId(String fEcId) {
        Map<String,Object> map = new HashMap<>(2);
        map.put("fEcId",fEcId);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        return productInfoMapper.selectCountAllByFEcId(map);
    }

    @Transactional
    @Override
    public int setEnable(String fId) {
        ProductInfo equipInfo = new ProductInfo();
        equipInfo.setfId(fId);
        equipInfo.setfPIsSerial(ProductParentEntity.IS_SERIAL_ENABLE);
        equipInfo.setfLastModifyTime(LocalDateTime.now());
        return productInfoMapper.updateOne(equipInfo);
    }

    @Transactional
    @Override
    public int setDisable(String fId) {
        ProductInfo equipInfo = new ProductInfo();
        equipInfo.setfId(fId);
        equipInfo.setfPIsSerial(ProductParentEntity.IS_SERIAL_DISABLE);
        equipInfo.setfLastModifyTime(LocalDateTime.now());
        return productInfoMapper.updateOne(equipInfo);
    }

    @Override
    public List<ProductInfoVo> selectEquipList() {
        Map<String,Object> map = new HashMap<>(2);
        map.put("fState",ParentEntity.FSTATE_DELETED);
        map.put("fProductCategory",ProductInfo.PRODUCT_CATEGORY_EQUIP);
        return productInfoMapper.selectEquipList(map);
    }
}
